

/* procedure that loads the data table in memory 
   and fill the array of the indexes
*/



//Point to the data base file 
#define TAB_TIME "times_for_tables.dat"
#define TAB_CUT  "star_cut.dat"          
#define TAB_MASS "mass_for_tables.dat"   
#define TAB_NUV  "log_nuv_table.dat"
#define TAB_FUV  "log_fuv_table.dat"
#define TAB_BOL  "log_bol_table.dat"
#define TAB_QHO  "log_qh0_table.dat"
#define TAB_J  "log_J_table.dat"
#define TAB_H  "log_H_table.dat"
#define TAB_K  "log_K_table.dat"
#define TAB_SDSSu  "log_SDSSu_table.dat"
#define TAB_SDSSg  "log_SDSSg_table.dat"
#define TAB_SDSSr  "log_SDSSr_table.dat"
#define TAB_SDSSi  "log_SDSSi_table.dat"
#define TAB_SDSSz  "log_SDSSz_table.dat"
#define TAB_johnsonU  "log_johnsonU_table.dat"
#define TAB_johnsonB  "log_johnsonB_table.dat"
#define TAB_johnsonV  "log_johnsonV_table.dat"
#define TAB_johnsonR  "log_johnsonR_table.dat"
#define TAB_johnsonI  "log_johnsonI_table.dat"
#define TAB_LOW_QHO  "lowmass_log_qh0.dat"  
#define TAB_LOW_MASS "lowmass_masses.dat"    
#define TAB_LOW_BOL  "lowmass_log_bol.dat"  
#define TAB_LOW_FUV  "lowmass_log_fuv.dat"  
#define TAB_LOW_NUV  "lowmass_log_nuv.dat"  
#define TAB_LOW_J  "lowmass_log_J.dat"  
#define TAB_LOW_H  "lowmass_log_H.dat"  
#define TAB_LOW_K  "lowmass_log_K.dat"  
#define TAB_LOW_SDSSu  "lowmass_log_SDSSu.dat"  
#define TAB_LOW_SDSSg  "lowmass_log_SDSSg.dat"  
#define TAB_LOW_SDSSr  "lowmass_log_SDSSr.dat"  
#define TAB_LOW_SDSSi  "lowmass_log_SDSSi.dat"  
#define TAB_LOW_SDSSz  "lowmass_log_SDSSz.dat"  
#define TAB_LOW_johnsonU  "lowmass_log_johnsonU.dat"  
#define TAB_LOW_johnsonB  "lowmass_log_johnsonB.dat"  
#define TAB_LOW_johnsonV  "lowmass_log_johnsonV.dat"  
#define TAB_LOW_johnsonR  "lowmass_log_johnsonR.dat"  
#define TAB_LOW_johnsonI  "lowmass_log_johnsonI.dat"

using namespace std;


//allow use of global variables from table
extern long which_fluxes;
extern vector<double> tab_time_axis;
extern vector<double> tab_time_axis_indx;
extern vector<double> tab_time_cutoff;
extern vector<double> tab_mass_axis;
extern vector<double> tab_low_mass_axis;

extern long ntables;
extern string data_dir;

void load_table(double ** flux_matrix, double ** low_matrix){
  
  vector<double> tab_nuv;
  vector<double> tab_fuv;
  vector<double> tab_bol;
  vector<double> tab_qho;
  vector<double> tab_J;
  vector<double> tab_H;
  vector<double> tab_K;
  vector<double> tab_SDSSu;
  vector<double> tab_SDSSg;
  vector<double> tab_SDSSr;
  vector<double> tab_SDSSi;
  vector<double> tab_SDSSz;
  vector<double> tab_johnsonU;
  vector<double> tab_johnsonB;
  vector<double> tab_johnsonV;
  vector<double> tab_johnsonR;
  vector<double> tab_johnsonI;
  vector<double> tab_low_qho;
  vector<double> tab_low_bol;
  vector<double> tab_low_fuv;
  vector<double> tab_low_nuv;
  vector<double> tab_low_J;
  vector<double> tab_low_H;
  vector<double> tab_low_K;
  vector<double> tab_low_SDSSu;
  vector<double> tab_low_SDSSg;
  vector<double> tab_low_SDSSr;
  vector<double> tab_low_SDSSi;
  vector<double> tab_low_SDSSz;
  vector<double> tab_low_johnsonU;
  vector<double> tab_low_johnsonB;
  vector<double> tab_low_johnsonV;
  vector<double> tab_low_johnsonR;
  vector<double> tab_low_johnsonI;
  double buffer;
  ifstream myfile;
  string tabfile;

  
  cout<<"LOAD_T: Loading stellar model tables... "<<endl;


  //Now load the tables
	//time axis, mass axis,s already allocated in check_table()
  

  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_CUT);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
 
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_time_cutoff.push_back(buffer);
  }
  myfile.close();
  

  //------------------------Read
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_MASS);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_mass_axis.push_back(buffer);
  }
  myfile.close();
  

  //make an array of indexes for time; used in 2D interpolation
  for(int i=0;i<(int) tab_time_axis.size();i++){
    tab_time_axis_indx.push_back(i);
  }


if((long)(which_fluxes & (long) pow((double)2,(double) 1)) > 0){

  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_NUV);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_nuv.push_back(buffer);
  }
  myfile.close();

  //------------------------Read
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_NUV);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_nuv.push_back(buffer);
  }
  myfile.close();
}

if((long)(which_fluxes & (long) pow((double)2,(double) 2)) > 0){
  
  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_FUV);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_fuv.push_back(buffer);
  }
  myfile.close();
  
  //------------------------Read
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_FUV);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_fuv.push_back(buffer);
  }
  myfile.close();
}

if((long)(which_fluxes & (long) pow((double)2,(double) 3)) > 0){   
  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_BOL);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_bol.push_back(buffer);
  }
  myfile.close();
 

  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_BOL);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_bol.push_back(buffer);
  }
  myfile.close();
}  
 
if((long)(which_fluxes & (long) pow((double)2,(double) 4)) > 0){
  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_QHO);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_qho.push_back(buffer);
  }
  myfile.close();

  

  //------------------------Read
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_QHO);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1); 
  } 
 
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_qho.push_back(buffer);
  }
  myfile.close();
}

//-------------------------------------------------------
//----------------------------Read in New fluxes
//-------------------------------------------------------
//------------------------Read 
if((long)(which_fluxes & (long) pow((double)2,(double) 5)) > 0){

  tabfile.assign(data_dir);
  tabfile.append(TAB_J);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_J.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_J);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_J.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 6)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_H);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_H.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_H);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_H.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 7)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_K);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_K.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_K);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_K.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 8)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_SDSSu);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_SDSSu.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_SDSSu);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_SDSSu.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 9)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_SDSSg);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_SDSSg.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_SDSSg);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_SDSSg.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 10)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_SDSSr);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_SDSSr.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_SDSSr);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_SDSSr.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 11)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_SDSSi);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_SDSSi.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_SDSSi);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_SDSSi.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 12)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_SDSSz);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_SDSSz.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_SDSSz);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_SDSSz.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 13)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_johnsonU);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_johnsonU.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_johnsonU);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_johnsonU.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 14)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_johnsonB);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_johnsonB.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_johnsonB);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_johnsonB.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 15)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_johnsonV);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_johnsonV.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_johnsonV);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_johnsonV.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 16)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_johnsonR);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_johnsonR.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_johnsonR);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_johnsonR.push_back(buffer);
  }
  myfile.close();
}


if((long)(which_fluxes & (long) pow((double)2,(double) 17)) > 0){

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_johnsonI);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) {
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_johnsonI.push_back(buffer);
  }
  myfile.close();

//------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_johnsonI);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table:"<<tabfile<<endl;
    exit(1);
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_johnsonI.push_back(buffer);
  }
  myfile.close();
}

 
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
  //this is the part where I put all the parsed information into the matrix...
  //find out how many different flux tables I need to read
  //------------The Fluxes-----------
  
  //all tables are same size for repeition of tab_nuv.size() isn't a problem
  long ct_tables=0;
  //starting with the main tables...
  for(long bit_ct2=1;bit_ct2<18;bit_ct2++) {
    if (bit_ct2 == 1 && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_nuv.size(); kk++)
	flux_matrix[ct_tables][kk]=tab_nuv[kk];
      for(long kk2=0; kk2<(long)tab_low_nuv.size(); kk2++)
	low_matrix[ct_tables][kk2]=tab_low_nuv[kk2];
      ct_tables++;
    }
    else if (bit_ct2 == 2 && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_fuv.size(); kk++)
	flux_matrix[ct_tables][kk]=tab_fuv[kk];
      for(long kk2=0; kk2<(long)tab_low_fuv.size(); kk2++)
	low_matrix[ct_tables][kk2]=tab_low_fuv[kk2];
       ct_tables++;
    }
    else if (bit_ct2 == 3 && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_bol.size(); kk++)
	flux_matrix[ct_tables][kk]=tab_bol[kk];
      for(long kk2=0; kk2<(long)tab_low_bol.size(); kk2++)
	low_matrix[ct_tables][kk2]=tab_low_bol[kk2];
      ct_tables++;      
    }
    else if (bit_ct2 ==4  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_qho.size(); kk++)
	flux_matrix[ct_tables][kk]=tab_qho[kk];
      for(long kk2=0; kk2<(long)tab_low_qho.size(); kk2++)
	low_matrix[ct_tables][kk2]=tab_low_qho[kk2];           
      ct_tables++;
    }
    else if (bit_ct2 ==5  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_J.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_J[kk];
      for(long kk2=0; kk2<(long)tab_low_J.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_J[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==6  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_H.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_H[kk];
      for(long kk2=0; kk2<(long)tab_low_H.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_H[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==7  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_K.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_K[kk];
      for(long kk2=0; kk2<(long)tab_low_K.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_K[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==8  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_SDSSu.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_SDSSu[kk];
      for(long kk2=0; kk2<(long)tab_low_SDSSu.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_SDSSu[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==9  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_SDSSg.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_SDSSg[kk];
      for(long kk2=0; kk2<(long)tab_low_SDSSg.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_SDSSg[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==10  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_SDSSr.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_SDSSr[kk];
      for(long kk2=0; kk2<(long)tab_low_SDSSr.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_SDSSr[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==11  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_SDSSi.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_SDSSi[kk];
      for(long kk2=0; kk2<(long)tab_low_SDSSi.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_SDSSi[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==12  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_SDSSz.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_SDSSz[kk];
      for(long kk2=0; kk2<(long)tab_low_SDSSz.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_SDSSz[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==13  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_johnsonU.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_johnsonU[kk];
      for(long kk2=0; kk2<(long)tab_low_johnsonU.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_johnsonU[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==14  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_johnsonB.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_johnsonB[kk];
      for(long kk2=0; kk2<(long)tab_low_johnsonB.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_johnsonB[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==15  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_johnsonV.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_johnsonV[kk];
      for(long kk2=0; kk2<(long)tab_low_johnsonV.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_johnsonV[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==16  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_johnsonR.size(); kk++)
        flux_matrix[ct_tables][kk]=tab_johnsonR[kk];
      for(long kk2=0; kk2<(long)tab_low_johnsonR.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_johnsonR[kk2];
      ct_tables++;
    }
    else if (bit_ct2 ==17  && (long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
      for(long kk=0; kk<(long)tab_johnsonI.size(); kk++)
	flux_matrix[ct_tables][kk]=tab_johnsonI[kk];
      for(long kk2=0; kk2<(long)tab_low_johnsonI.size(); kk2++)
        low_matrix[ct_tables][kk2]=tab_low_johnsonI[kk2];
      ct_tables++;
    }

    //    if((long)(which_fluxes & (long) pow((double)2,(double) bit_ct2)) > 0){
    //      for(long hmat=0;hmat<(long)tab_nuv.size();hmat++){
    //      flux_matrix[ct_tables][hmat]=buffer2[hmat];
    //      }
    //      ct_tables++;
  }
  
  return;
}
